export const UNSELECTED_SKU_VALUE_ID = '';
export const LIMIT_TYPE = {
    QUOTA_LIMIT: 0,
    STOCK_LIMIT: 1
};
// utils
export const skuToast = (text) => { // Toast轻提示(待丰富，根据type再封装)
    uni.showToast({
        icon: 'none',
        title: text
    })
}
export const isDef = (value) => {
    return value !== undefined && value !== null;
}
export const isObj = (x) => {
    const type = typeof x;
    return x !== null && (type === 'object' || type === 'function');
}
export const isNumber = (value) => {
    return /^\d+$/.test(value);
}
export const range = (num, min, max) => {
    return Math.min(Math.max(num, min), max);
}

/*
  normalize sku tree

  [
    {
      count: 2,
      k: "品种", // 规格名称 skuKeyName
      k_id: "1200", // skuKeyId
      k_s: "s1" // skuKeyStr
      v: [ // skuValues
        { // skuValue
          id: "1201", // skuValueId
          name: "萌" // 具体的规格值 skuValueName
        }, {
          id: "973",
          name: "帅"
        }
      ]
    },
    ...
  ]
                |
                v
  {
    s1: [{
      id: "1201",
      name: "萌"
    }, {
      id: "973",
      name: "帅"
    }],
    ...
  }
 */
export const normalizeSkuTree = skuTree => {
    if (!skuTree) return;
    const normalizedTree = {};
    skuTree.forEach(treeItem => {
        normalizedTree[treeItem.k_s] = treeItem.v;
    });
    return normalizedTree;
};

// 判断是否所有的sku都已经选中
export const isAllSelected = (skuTree, selectedSku) => {
    if (!skuTree) return;
    // 筛选selectedSku对象中key值不为空的值
    const selected = Object.keys(selectedSku).filter(
        skuKeyStr => selectedSku[skuKeyStr] !== UNSELECTED_SKU_VALUE_ID
    );
    return skuTree.length === selected.length;
};

// 根据已选择的 sku 获取 skuComb
export const getSkuComb = (skuList, selectedSku) => {
    const skuComb = skuList.filter(item => (
        Object.keys(selectedSku).every(
            skuKeyStr => String(item[skuKeyStr]) === String(selectedSku[skuKeyStr])
        )
    ));
    return skuComb[0];
};

// 获取已选择的sku名称
export const getSelectedSkuValues = (skuTree, selectedSku) => {
    if (!skuTree || !selectedSku) return;
    const normalizedTree = normalizeSkuTree(skuTree);
    return Object.keys(selectedSku).reduce((selectedValues, skuKeyStr) => {
        const skuValues = normalizedTree[skuKeyStr];
        const skuValueId = selectedSku[skuKeyStr];

        if (skuValueId !== UNSELECTED_SKU_VALUE_ID) {
            const skuValue = skuValues.filter(value => value.id === skuValueId)[0];
            skuValue && selectedValues.push(skuValue);
        }
        return selectedValues;
    }, []);
};

// 获取已选择的sku图片
export const getSelectedSkuImg = (skuList, selectedSku) => {
    var s1 = selectedSku.s1 || 0;
    var s2 = selectedSku.s2 || 0;
    var s3 = selectedSku.s3 || 0;
    var result = '';
    if (s1) {
        const this_sku = skuList.filter(function(item) {
            return item.s1 == s1 && item.s2 == s2 && item.s3 == s3;
        })[0] || {};
        if (typeof this_sku.spec_image != 'undefined') {
            result = this_sku.spec_image;
        }
    }
    return result;
};

// 判断sku是否可选
export const isSkuChoosable = (skuList, selectedSku, skuToChoose) => {
    const {
        key,
        valueId
    } = skuToChoose;

    // 先假设sku已选中，拼入已选中sku对象中
    const matchedSku = Object.assign({}, selectedSku, {
        [key]: valueId
    });

    // 再判断剩余sku是否全部不可选，若不可选则当前sku不可选中
    const skusToCheck = Object.keys(matchedSku).filter(
        skuKey => matchedSku[skuKey] !== UNSELECTED_SKU_VALUE_ID
    );

    const filteredSku = skuList.filter(sku => (
        skusToCheck.every(
            skuKey => String(matchedSku[skuKey]) === String(sku[skuKey])
        )
    ));

    const stock = filteredSku.reduce((total, sku) => {
        total += sku.stock_num;
        return total;
    }, 0);
    return stock > 0;
};

export default {
    UNSELECTED_SKU_VALUE_ID,
    LIMIT_TYPE,

    skuToast,
    isDef,
    isObj,
    isNumber,
    range,


    normalizeSkuTree,
    getSkuComb,
    getSelectedSkuValues,
    getSelectedSkuImg,
    isAllSelected,
    isSkuChoosable
};
